package data_overview

import (
	"math/rand"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/variable"
	"peilian-api/app/model"
	"peilian-api/app/services/view_service"
	"peilian-api/utils/format"
	"peilian-api/utils/tools"
	"time"

	"github.com/gin-gonic/gin"
)

func DataView(ctx *gin.Context) {
	query := new(view_service.Query)
	if err := ctx.ShouldBindQuery(query); err != nil {
		tools.Resp400(ctx, "查询字符串参数有问题: "+err.Error())
		return
	}
	if err := variable.Validator.Struct(query); err != nil {
		tools.Resp400(ctx, "查询字符串参数校验失败: "+err.Error())
		return
	}
	uid, exist := ctx.Get("uid")
	if !exist {
		tools.Resp400(ctx, "获取用户失败")
		return
	}
	// 根据uid查询user库
	us, _ := tables.GetAgentInfoByUid(uid.(uint))
	// 设置agentId
	//if utype, exists := ctx.Get("utype"); exists {
	//	ut := utype.(string)
	// 如果用户不是超级用户agent_id设为0
	if us.Type != tables.UserTypeSadmin {
		query.AgentId = int(us.AgentID)
		if err := query.SetTopConstitute(ctx); err != nil {
			return
		}
	}
	query.LoginUserType = us.Type
	//}

	switch query.Type {
	case "count":
		resp, err := query.Count(ctx)
		if err != nil {
			tools.Resp400(ctx, err.Error())
			return
		}
		tools.RespOk(ctx, resp)
	case "total":
		resp, err := query.Total(ctx)
		if err != nil {
			tools.Resp400(ctx, err.Error())
			return
		}
		tools.RespOk(ctx, resp)
	case "pro:pos", "pro:neg", "gen:pos", "gen:neg", "experience:pos", "experience:neg",
		"tool", "personality", "career_recommend":
		resp, err := query.EsSearchLabel(query.Type, false)
		if err != nil {
			tools.Resp400(ctx, err.Error())
			return
		}
		tools.RespOk(ctx, resp)
	case "interview_state":
		resp, err := query.EsSearchState()
		if err != nil {
			tools.Resp400(ctx, err.Error())
			return
		}
		tools.RespOk(ctx, resp)
	case "condition":
		if err := query.Condition(ctx); err != nil {
			tools.Resp400(ctx, err.Error())
			return
		}
		resp := map[string]interface{}{
			"type":       query.Constitute.Type,
			"constitute": query.MemberInfo,
			"career":     query.CareerInfo,
			"count":      model.Count{},
		}
		tools.RespOk(ctx, resp)
	case "export":
		url, err := query.EsDataExport(ctx)
		if err != nil {
			tools.Resp400(ctx, err.Error())
			return
		}
		format.NewResponseJson(ctx).SuccessObj(struct {
			Url string `json:"url"`
		}{Url: url})
		return
	case "personal_export":
		url, err := query.EsDataPersonalExport(ctx)
		if err != nil {
			tools.Resp400(ctx, err.Error())
			return
		}
		format.NewResponseJson(ctx).SuccessObj(struct {
			Url string `json:"url"`
		}{Url: url})
		return
	default:
		tools.Resp400(ctx, "type参数有误")
	}

}

func Online(ctx *gin.Context) {
	// 获取今日在线数据总量
	s := time.Now().Format("2006-01-02")
	var count int64 = 0
	d := variable.DB.MySQL.Model(&tables.User{}).Where("type in (?, ?) and last_login > ? and last_login < ?", tables.UserTypeAnonymous, tables.UserTypeNormal, s+" 00:00:00", s+" 23:59:59").Count(&count)
	if d.Error != nil {
		format.NewResponseJson(ctx).Err(30002, d.Error.Error())
		return
	}

	format.NewResponseJson(ctx).SuccessObj(map[string]int64{"count": 600 + count*37 + int64(rand.Intn(9))})
}
